var util = require("../../utils/util.js");
var api = require('../../config/api.js');


//获取应用实例
var app = getApp()
Page({
  data: {
    kemu:'',
    resultlist: '',
    resultlists: '',
    orderNum: 1,
    id: '',
    entity: '',
    flag: [5, 0, 0], //每行标记变量 改变显示的五角星
    startext: ['', '', ''], //各个下标对应每行显示  5个评价中的一个 非常不满意/不满意
    stardata: [1, 2, 3, 4, 5], //每颗星星对应的数值  决定了渲染出几颗五角星
    content: '',
    star: '5',
    wq:[],
    disabled:false
  },
  onLoad: function (options) {
    var id = options.id; //获取参数
    var that = this;
    that.setData({
      id: id
    })
    //console.log("eval:::::id::::::"+id);
    that.queryCoachEvalDetail(id);

    util.request(api.queryAllWq, {}).then(function (res) {
      //console.log("res:::::::"+JSON.stringify(res));

      that.setData({
        resultlist: res.data.lq,
        resultlists: res.data.ls
      })
    }) 
  },
  
  clearWq(){
    util.request(api.clearWq,{id:this.data.id}).then(function(res){
      
    })
  },
  queryCoachEvalDetail(id) {
    var that = this;
    util.request(api.queryCoachEvalDetail, {
      id: id
    }).then(function (res) {
      //console.log("vvvvvvvvvvvvvv:::::"+JSON.stringify(res.data));
      var gender = res.data.gender;
      if (gender == '1') {
        gender = '男'
      } else {
        gender = '女'
      }
      var subject = res.data.subject
      if (subject == '1') {
        subject = '科目一'
      } else if (subject == '2') {
        subject = '科目二'
      } else if (subject == '3') {
        subject = '科目三'
      } else if (subject == '4') {
        subject = '文明驾驶'
      }
      that.setData({
        entity: res.data,
        kemu:res.data.subject,
        ['entity.gender']: gender,
        ['entity.subject']: subject
      })
    })
  },
  radioChange(e) {
    // console.log(JSON.stringify(e));
    const checked = e.detail.value;
    this.data.wq.push({"wqid":checked});
    // var id = this.data.id;
    // var that = this;
    /*util.request(api.saveCoachEvalWq, {
      id: id,
      wqid: checked
    }).then(function (res) {

    })*/
  },

  tapEvent() {
    console.log('按钮被点击')
  },
  // 五星评价事件
  changeColor: function (e) {
    var index = e.currentTarget.dataset.index; //判断选中了哪个评价 在前端页面写死的索引
    var num = e.currentTarget.dataset.no; //前端页面的 item 传来选择了多少个
    var a = 'flag[' + index + ']';
    var b = 'startext[' + index + ']';

    var that = this;
    if (num == 1) {
      that.setData({
        [a]: 1,
        [b]: '非常不满意'
      });
    } else if (num == 2) {
      that.setData({
        [a]: 2,
        [b]: '不满意'
      });
    } else if (num == 3) {
      that.setData({
        [a]: 3,
        [b]: '一般'
      });
    } else if (num == 4) {
      that.setData({
        [a]: 4,
        [b]: '满意'
      });
    } else if (num == 5) {
      that.setData({
        [a]: 5,
        [b]: '非常满意'
      });
    }
    that.setData({
      star: num
    })
    //进行更新星级评价
    /*var id = this.data.id;
    util.request(api.updateStarRating, {id:id,starRatinig:num}).then(function (res) {
      
    })*/

  },
  ifshowArea(e) {
    var t_show = e.currentTarget.dataset.show == "yes" ? true : false;
    if (t_show) { //不显示textarea
      this.setData({
        areatext: this.data.u_remark ? this.data.u_remark : "订单备注(0/100)",
        areaHeight: 'margin-bottom: 90rpx;'
      });
      this.setData({
        multiShow: t_show
      })
    } else { //显示textarea
      this.setData({
        areaHeight: ' margin-bottom: 250rpx;'
      });
      wx.createSelectorQuery().select('.j_page').boundingClientRect((rect) => {
        console.log(rect)
        // 使页面滚动到底部
        wx.pageScrollTo({
          scrollTop: rect.bottom
        })
        this.setData({
          multiShow: t_show
        })
      }).exec()
    }
  },
  inputChange: function (e) {
    //console.log('e.detail.value:::::::'+e.detail.value);
    this.setData({
      content: e.detail.value.replace(/\s+/g, ''),
    });

  },
  goSave: function () {
    var that = this;
    //教练满意率星星,默认为5
    var star = that.data.star;
    if (star == '') {
      wx.showModal({
        title: '提示',
        content: '请选择教练满意度！',
        showCancel: false
      })
    return;
    }
    var content = that.data.content;
      if (content == '') {
        wx.showModal({
          title: '提示',
          content: '请填写教练评价！',
          showCancel: false
        })
        return;
      }
      that.setData({
        disabled: true
      })
        //保存
        wx.showLoading({
          title: '正在保存中',
        });
        let timer = setTimeout(function() {
          wx.hideLoading();
          wx.showToast({
            title: '请求异常，请重试！',
            icon: 'none',
            duration: 2000
          });
          that.setData({
            disabled: false
          })
        }, 15000);//15秒
        util.request(api.updateContent, {
          subject:that.data.kemu,
          id: that.data.id,
          content: that.data.content,
          star: that.data.star,
          wq:this.data.wq
        }).then(function (res) {
          //console.log(JSON.stringify(res));
          if(res.data.status == '0'){
            //清除定时器
            clearTimeout(timer);
            wx.showToast({
              title: res.data.msg,
              icon:'none',
              duration: 3000
            })
            that.setData({
              disabled: false
            })
          }
          if(res.data.status == '1'){
            clearTimeout(timer);
            //弹出保存成功
            wx.showToast({
              title: '保存成功！',
              icon: 'success',
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }, 1000)
              },
            })
          }
        })
  },
  onShow: function () {
    //清空问卷信息
    this.clearWq();
  },
})